package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.system.dl.DeleteImages;
import com.ruoyi.system.domain.Hotel;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AttractionsMapper;
import com.ruoyi.system.domain.Attractions;
import com.ruoyi.system.service.IAttractionsService;

/**
 * 景点Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class AttractionsServiceImpl implements IAttractionsService 
{
    @Autowired
    private AttractionsMapper attractionsMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 查询景点
     * 
     * @param id 景点主键
     * @return 景点
     */
    @Override
    public Attractions selectAttractionsById(Long id) {
        Attractions attractions = attractionsMapper.selectAttractionsById(id);
        attractions.setAttractionsImages(imagesMapper.selectImages("attractions"+id));
        return attractions;
    }

    /**
     * 查询景点列表
     * 
     * @param attractions 景点
     * @return 景点
     */
    @Override
    public List<Attractions> selectAttractionsList(Attractions attractions) {
        List<Attractions> attractiones = attractionsMapper.selectAttractionsList(attractions);

        return attractiones.stream().peek(hot -> {
            hot.setAttractionsImages(imagesMapper.selectImages("attractions"+hot.getId()));
        }).collect(Collectors.toList());
    }

    /**
     * 新增景点
     * 
     * @param attractions 景点
     * @return 结果
     */
    @Override
    public int insertAttractions(Attractions attractions) {
        int i = attractionsMapper.insertAttractions(attractions);

        List<Attractions> attractions1 = attractionsMapper.selectAttractionsList(attractions);
        Long id = attractions1.get(0).getId();

        List<String> attractionsImages = attractions.getAttractionsImages();

        attractionsImages.forEach(o->imagesMapper.insertSelective(new Images(null,"attractions"+id,o)));

        return i;
    }

    /**
     * 修改景点
     * 
     * @param attractions 景点
     * @return 结果
     */
    @Override
    public int updateAttractions(Attractions attractions) {

        int i = attractionsMapper.updateAttractions(attractions);
        String foreignId = "attractions" + attractions.getId();

        // 将所有与该id的图片删除
        imagesMapper.deleteByForeginId(foreignId);

        DeleteImages.deleteIma(foreignId,imagesMapper);

        Set<String> strings = new HashSet<>(attractions.getAttractionsImages());

        // 添加
        strings.forEach(o -> imagesMapper.insert(new Images(null,foreignId,o)));

        return i;
    }

    /**
     * 批量删除景点
     * 
     * @param ids 需要删除的景点主键
     * @return 结果
     */
    @Override
    public int deleteAttractionsByIds(Long[] ids) {
        Arrays.stream(ids).forEach(o -> {
            imagesMapper.deleteByForeginId("attractions"+o);
            DeleteImages.deleteIma("attractions"+o,imagesMapper);
        });
        return attractionsMapper.deleteAttractionsByIds(ids);
    }

    /**
     * 删除景点信息
     * 
     * @param id 景点主键
     * @return 结果
     */
    @Override
    public int deleteAttractionsById(Long id) {
        String foreignId = "attractions"+id;
        DeleteImages.deleteIma(foreignId,imagesMapper);
        imagesMapper.deleteByForeginId(foreignId);
        return attractionsMapper.deleteAttractionsById(id);
    }
}
